package rdd.dep;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.List;

public class Spark8485_Dep {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf();
        conf.setMaster("local[*]");
        conf.setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);

        final List<Tuple2<String,Integer>> datas = new ArrayList<>();
        datas.add(new Tuple2<String,Integer>("a",1));
        datas.add(new Tuple2<String,Integer>("a",2));
        datas.add(new Tuple2<String,Integer>("a",3));
        datas.add(new Tuple2<String,Integer>("a",4));

        final JavaRDD<Tuple2<String,Integer>> rdd = jsc.parallelize(datas);
        //查看血缘关系
        System.out.println(rdd.toDebugString());
        //查看依赖关系
        System.out.println(rdd.rdd().dependencies());

        final JavaPairRDD<String,Integer> pairRDD = rdd.mapToPair(t -> t);
        //查看血缘关系
        System.out.println(pairRDD.toDebugString());
        //查看依赖关系
        System.out.println(pairRDD.rdd().dependencies());


        final JavaPairRDD<String, Integer> pairRDD1 = pairRDD.reduceByKey(
                Integer::sum
        );
        //查看血缘关系
        System.out.println(pairRDD1.toDebugString());
        //查看依赖关系
        System.out.println(pairRDD1.rdd().dependencies());


    }
}
